package com.tuyrt.firstcode.kotlin.eg

import kotlin.reflect.KProperty

/**
 * Created by tuyrt7 on 2021/4/12.
 *  委托代理实现懒加载赋值
 */
fun <T> later(block: () -> T) = Later(block)

class Later<T>(val block: () -> T) {
    var value: Any? = null
    operator fun getValue(any: Any?, prop: KProperty<*>): T {
        if (value == null) {
            value = block()
        }
        return value as T
    }

}


